﻿@using RMS.EF.Model
@model List<RMS_Role_Menu>
@{
    var programs = ViewBag.Programs as List<RMS_Program>;
    var errorMsg = ViewBag.ErrorMsg as string;
    if (!string.IsNullOrWhiteSpace(errorMsg))
    {
        <script>
            toastr.error("@(errorMsg)");
        </script>
    }
}

<link href="~/bower_components/zTree/css/zTreeStyle/metro.css" rel="stylesheet" />
<div class="form-horizontal">

    <div class="panel panel-default">
        <div class="panel-heading">搜索栏</div>
        <div class="panel-body">
            <form id="box_searchForm" class="form-inline" role="form" onsubmit="box_search(); return false;">
                <div class="form-group">
                    <label for="programId">程序</label>
                    <select id="programId" name="programId" class="form-control" onchange="treeGetData()">
                        @for (int i = 0; i < programs.Count; i++)
                        {
                            <option value="@programs[i].Id">@programs[i].Name</option>
                        }
                    </select>
                </div>
                <div class="form-group">
                    <label for="searchValue_nodata">搜索</label>
                    <input type="text" class="form-control" id="searchValue_nodata" name="searchValue_nodata" maxlength="50" placeholder="名称和链接" />
                </div>
                <input type="hidden" name="type_m" value="@(E_Type_Menu.All)" />
                <input type="hidden" name="status" value="@(E_Status.Enable)" />
                <button type="submit" class="btn btn-primary">搜索</button>
            </form>
        </div>
    </div>
    <div class="dataTable_wrapper"style="overflow: auto;max-height: 300px;">
        <ul id="box_mainTree" class="ztree" style="overflow:auto;"></ul>
    </div>
    <form id="saveform" method="post" action="@Url.Action("Relation_Menu")">
        <input type="hidden" name="id" value="@ViewBag.Id" />
        <input type="hidden" id="hidProgramId" name="programId" value="" />
        <input type="hidden" id="menuId" name="menuId" value="" />
    </form>
</div>

<script src="~/bower_components/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<script src="~/Scripts/Common/ztree_extend.js?v=2"></script>
<script>

    var box_tableurl = "@Url.Action("ListGetData","Menu",new { area= "RMS" })";
    var menuurl = "@Url.Action("Get_RoleMenu_ByRoleIdAndProjectId")";
    var enable = "@((int)E_Status.Enable)";
    var type_menu = "@((int)E_Type_Menu.Menu)";
    var type_function = "@((int)E_Type_Menu.Function)";
    var checkmenuid = [];
    var id="@ViewBag.Id";
    //树形视图初始化
    $(function () {
        $("#box_searchForm .form-group").css("margin-right", 0).css("margin-left", 0);

        treeGetData();

        $("#btnSave").unbind();
        $("#modal-content").unbind();
        $("#btnSave").click(function () {
            save();
        });
        $("#modal-content").submit(function () {
            save();
            return false;
        });
    });

    function treeGetData(){
        loading.start();
        var data = {};
        $("#box_searchForm").serializeArray().map(function (x) { data[x.name] = x.value; });
        $.ajax({
            url: box_tableurl,
            data: data,
            type: "POST",
            dataType: "JSON",
            success: function (treedata) {
                if (!treedata.IsSuccess) {
                    toastr.error(treedata.Message);
                    return;
                }
                $.ajax({
                    async: false,
                    url: menuurl,
                    data: { id: id, programId: $("#programId").val() },
                    type: "POST",
                    dataType: "JSON",
                    success: function (data) {
                        if (data) {
                            checkmenuid=data;
                        }
                    }
                });
                //初始赋值
                for (var i = 0; i < treedata.Data.length; i++) {
                    if($.inArray(treedata.Data[i].Id,checkmenuid)!=-1){
                        treedata.Data[i].checked=true;
                    }else {
                        treedata.Data[i].checked=false;
                    }
                }
                //控件初始化
                var setting = {
                    check: {
                        enable: true
                    },
                    view: {
                        fontCss: setFontCss_ztree
                    },
                    data: {
                        key: {
                            name: "Name"
                        },
                        simpleData: {
                            enable: true,
                            idKey: "Id",
                            pIdKey: "ParentId",
                        }
                    }
                };
                $.fn.zTree.init($("#box_mainTree"), setting, treedata.Data);
            },
            error: function (xhr, status, error) {
                alert("error");
            },
            complete:function(){
                loading.end();
            }
        });
    }
    //搜索
    function box_search() {
        $.fn.zTree.search("box_mainTree","Name",$('#searchValue_nodata').val(), "");
    }
    //保存
    function save() {
        var nodeIds = [];
        var treeObj = $.fn.zTree.getZTreeObj("box_mainTree");
        var nodes = treeObj.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            nodeIds.push(nodes[i].Id);
        }
        $("#menuId").val(nodeIds);
        $("#hidProgramId").val($("#programId").val())
        $("#saveform").formSave({
            success: function (result) {
                if (result.IsSuccess) {
                    toastr.success(result.Message);
                } else {
                    toastr.error(result.Message);
                }
            }
        });
    }
</script>